var entity = {
    entityId: ""
};


var contraindication = {
    id: "",
    table: null,
    seItem: null,		//选中的条目
    layerIndex: -1,

};


contraindication.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {title: '禁忌症', field: 'name', align: 'left', sortable: false},
        {title: '代码', field: 'code', align: 'left', sortable: false},
        {title: '描述', field: 'info', align: 'left', sortable: false},
        {title: '文献来源', field: 'source', align: 'left', sortable: false},
        {
            title: '是否启用', field: 'active', align: 'center', width: '15%', sortable: false,
            formatter(value) {
                if (value == 1) return "<span class='label label-success'>是</span>";
                else return "<span class='label label-warning'>否</span>";
            }
        },
    ];
};
//按禁忌症名称查询
contraindication.query = function () {
    var params = {
        url: Sys.backendPath + "/sdc/contraindication/query",
        silent: false,
        query: {
            name: $('#contraindicationMap').val(),
        }
    };
    contraindication.table.refresh(params);
    contraindicationItem.change();
};

contraindication.refresh = function (){
    var params = {
        url: Sys.backendPath + "/sdc/contraindication/query",
        silent: false,
        query: {
            name: ''
        }
    };
    contraindication.table.refresh(params);
    contraindication.change();
}



//点击禁忌症
contraindication.onSelect = function (rowData) {
    if (contraindication.id != rowData.id) {
        contraindication.id = rowData.id;
        contraindication.change();
    }
}
// 点击禁忌症后触发  查询禁忌症条目
contraindication.change = function () {

    var param = {
        url: Sys.backendPath + "/sdc/contraindicationitem/listPage",
        silent: false,
        query: {
            contraindicationId: contraindication.id
        }
    }
    contraindicationItem.table.refresh(param)
}

//添加禁忌症
contraindication.add = function () {

    var index = Sys.open('新增禁忌症', Sys.backendPath + '/sdc/contraindication/to_add', ['800px', '350px']);
    this.layerIndex = index;
}


//修改禁忌症
contraindication.edit = function () {
    var contraindicationId = contraindication.id;
    if (contraindicationId == "" || contraindicationId == null) {
        Sys.info("请选择禁忌症");
        return false;
    }

    var index = Sys.open('修改禁忌症', Sys.backendPath + '/sdc/contraindication/to_update/' + contraindicationId, ['800px', '350px']);
    this.layerIndex = index;

}
//删除禁忌症
contraindication.delete = function () {
    var contraindicationId = contraindication.id

    if (contraindicationId == "" || contraindicationId == null) {
        Sys.info("请选择禁忌症");
        return false;
    }

    var operation = function () {
        var ajax = new $ax(Sys.backendPath + "/sdc/contraindication/delete", function (res) {
            Sys.success("删除成功");

            console.info("res" + res)
            contraindication.refresh();
        }, function (data) {
            console.info("data" + data)
            Sys.error("删除失败!");
        });
        ajax.set("contraindicationId", contraindicationId);
        ajax.start();
    }
    Sys.confirm("是否删除?", operation);

}

var contraindicationItem = {
    id: "",
    table: null,
    seItem: null,		//选中的条目
    layerIndex: -1,
};

contraindicationItem.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {field: 'itemName', title: '禁忌症条目',  align: 'left',  sortable: false},
        {field: 'expression', title: '条目规则',align: 'left',  resizable: 'false', sortable: false},
        {field: 'active', title: '是否有效', align: 'left',  resizable: 'false', sortable: false,
            formatter(value) {
                if (value == 1) return "<span class='label label-success'>是</span>";
                else return "<span class='label label-warning'>否</span>";
            }
        }
    ]
};

contraindicationItem.onSelect = function (rowData) {
    contraindicationItem.id = rowData.id;
};

// 添加条目
contraindicationItem.add = function () {

    if (contraindication.id == "" || contraindication.id == null) {
        Sys.info("请选择禁忌症");
        return false;
    }

    var index = Sys.open('新增禁忌症条目', Sys.backendPath + '/sdc/contraindicationitem/to_add/' + contraindication.id, ['80%', '100%']);
    this.layerIndex = index;


}
// 修改条目
contraindicationItem.edit = function () {

    if (contraindicationItem.id == "" || contraindicationItem.id == null) {
        Sys.info("请选择禁忌症条目");
        return false;
    }
    var index = Sys.open('修改条目', Sys.backendPath + '/sdc/contraindicationitem/to_update/' + contraindicationItem.id, ['80%', '100%']);
    this.layerIndex = index;

}


//删除条目
contraindicationItem.delete = function () {

    if (contraindicationItem.id == "" || contraindicationItem.id == null) {
        Sys.info("请选择禁忌症条目");
        return false;
    }
    var operation = function () {
        var ajax = new $ax(Sys.backendPath + "/sdc/contraindicationitem/delete", function (data) {
            Sys.success("删除成功!");
            contraindication.change();
        }, function (data) {
            Sys.error("删除失败!" + data.responseJSON.message + "!");
        });
        ajax.set("contraindicationItemId", contraindicationItem.id);
        ajax.start();
    };
    Sys.confirm("是否删除该条目?", operation);
}



$(function () {
    //查询并初始化禁忌症表格
    var initColumn1 = contraindication.initColumn();
    var contable = new CTDBSTable("contraindicationTable", Sys.backendPath + "/sdc/contraindication/query", initColumn1);
    contable.setOnClickRow(contraindication.onSelect)
    contable.setPageSize(15);
    contable.setShowExport(false);
    contable.setShowRefresh(false);
    contable.setShowColumns(false);
    contable.setShowToggle(false);
    contraindication.table = contable.init();

    var initColumn2 = contraindicationItem.initColumn();
    var itemtable = new CTDBSTable("contraindicationItemTable", "", initColumn2);
    itemtable.setOnClickRow(contraindicationItem.onSelect);
    itemtable.setShowToggle(false);
    itemtable.setPageSize(15);
    contraindicationItem.table = itemtable.init();


});
// 为redio添加点击事件
$('#contraindicationTable').on('check.bs.table', function (e, row, $element) {
    contraindication.onSelect(row);
})
$('#contraindicationItemTable').on('check.bs.table', function (e, row, $element) {
    contraindicationItem.onSelect(row);
})

function Common_GetValue() {
    var itmValue = '';

    if (typeof (arguments[0]) !== 'string') return '';
    if (arguments[0] == '') return '';

    var $this = $('#' + arguments[0]);
    if ($this.length < 1) return '';

    var className = $this.attr("class").split(' ')[0];

    if (className == 'textbox') {  //文本框
        itmValue = $this.val();
    } else if (className == 'hisui-numberbox') {  //数字
        itmValue = $this.val();
    } else if (className == 'hisui-datebox') {  //日期
        itmValue = $this.datebox('getValue');
    } else if (className == 'hisui-timespinner') {  //时间框
        itmValue = $this.timespinner('getValue');
    } else if (className == 'hisui-combobox') {  //下拉框（多选下拉框没有封装）
        itmValue = $this.combobox('getValue');
    } else if (className == 'hisui-switchbox') {  //开关
        itmValue = $this.switchbox('getValue');
    } else if (className == 'hisui-checkbox') {  // 单个复选框
        itmV3alue = $this.checkbox('getValue');
    } else if (className == 'hisui-radio') {  //单个单选框
        itmValue = $this.radio('getValue');
    } else if (className == 'hisui-searchbox') {  //查询框框
        itmValue = $this.searchbox('getValue');
    }

    return itmValue;
}
